package com.chixing.controller;

import com.chixing.entity.CommentDoc;
import com.chixing.service.ICommentDocService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
// 明确指定跨域配置
@CrossOrigin(
        origins = "http://localhost:5173",  // 允许的前端源
        methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.OPTIONS},  // 允许的方法
        allowCredentials = "true"  // 允许携带凭证
)
@RestController
@RequestMapping("/comments")
public class CommentDocController {

    @Autowired
    private ICommentDocService commentDocService;

    /**
     * 发布一级评论
     */
    @PostMapping("/movie/{movieId}")
    public Result postComment(@PathVariable Integer movieId,
                                          @RequestBody CommentDoc commentDoc) {
        commentDoc.setMovieId(movieId);
        return commentDocService.postComment(commentDoc);
    }

    /**
     * 回复评论
     */
    @PostMapping("/{parentId}/reply")
    public Result replyComment(@PathVariable String parentId,
                                           @RequestBody CommentDoc commentDoc) {
        return commentDocService.replyComment(parentId, commentDoc);
    }

    /**
     * 点赞评论
     */
    @PutMapping("/{commentId}/like")
    public Result likeComment(@PathVariable String commentId) {
        return commentDocService.likeComment(commentId);
    }

    /**
     * 获取电影评论列表（分页）
     */
    @GetMapping("/movie/{movieId}")
    public Result getCommentsByMovieId(@PathVariable Integer movieId,
                                                         @RequestParam(defaultValue = "0") int page,
                                                         @RequestParam(defaultValue = "10") int size) {
        return commentDocService.getCommentsByMovieId(movieId, page, size);
    }
}
